#include<string>
#include<stack>
using std::string;
using std::stack;

class Solution {
public:
    bool isValid(string s) {
        stack<char> myStack;
        for (auto c: s) {
            if (myStack.empty() || !this->canMerge(myStack.top(), c)) myStack.push(c);
            else myStack.pop();
        }
        return myStack.empty();
    }

    bool canMerge(char a, char b) {
        return ((a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}'));
    }
};